from app.extensions import db
from sqlalchemy import SmallInteger, Integer, String, Float, DateTime


class WeatherData(db.Model):
    __tablename__ = 'weather_data'

    id = db.Column(Integer, primary_key=True)
    record_date = db.Column(DateTime, nullable=False, unique=True)
    year = db.Column(SmallInteger)
    month = db.Column(SmallInteger)  # 修改为 SmallInteger，更通用
    day = db.Column(SmallInteger)  # 修改为 SmallInteger，更通用
    season = db.Column(String(50))
    guilin_temp = db.Column(Float)
    guilin_max = db.Column(Float)
    guilin_min = db.Column(Float)
    guilin_dewp = db.Column(Float)
    guilin_prcp = db.Column(Float)
    guilin_slp = db.Column(Float)
    guilin_visib = db.Column(Float)
    guilin_wdsp = db.Column(Float)
    guilin_mxspd = db.Column(Float)
    liuzhou_max = db.Column(Float)
    liuzhou_min = db.Column(Float)
    hezhou_max = db.Column(Float)
    hezhou_min = db.Column(Float)

    def to_dict(self):
        """将模型对象转换为字典"""
        return {
            'id': self.id,
            'record_date': self.record_date.strftime('%Y-%m-%d %H:%M:%S'),
            'year': self.year,
            'month': self.month,
            'day': self.day,
            'season': self.season,
            'guilin_temp': self.guilin_temp,
            'guilin_max': self.guilin_max,
            'guilin_min': self.guilin_min,
            'guilin_dewp': self.guilin_dewp,
            'guilin_prcp': self.guilin_prcp,
            'guilin_slp': self.guilin_slp,
            'guilin_visib': self.guilin_visib,
            'guilin_wdsp': self.guilin_wdsp,
            'guilin_mxspd': self.guilin_mxspd,
            'liuzhou_max': self.liuzhou_max,
            'liuzhou_min': self.liuzhou_min,
            'hezhou_max': self.hezhou_max,
            'hezhou_min': self.hezhou_min
        }
